package com.ibest.base.expand;

import com.alibaba.fastjson.JSONObject;
import com.ibest.base.entity.CanalBinlogBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class HandleInterceptEntrance {

    @Autowired
    private Map<String, Handler> handleInterceptBeanMap = new ConcurrentHashMap<>();

    //业务链式处理器list
    private static List<Handler> handleInterceptList = new ArrayList<>();

    public void handleEntrance(CanalBinlogBean object) {
        if (CollectionUtils.isEmpty(handleInterceptList)) {
            getHandleInterceptList();
        }
        for (Handler handler : handleInterceptList) {
                //要注入参数，可以在此处理
                handler.handle(object);
        }
    }

    @PostConstruct
    private synchronized void getHandleInterceptList () {
        log.info("处理拦截集合：{}",JSONObject.toJSONString(handleInterceptBeanMap));
        List<Handler> handleTemp = new ArrayList<>();
        for (String beanName : handleInterceptBeanMap.keySet()) {
            Handler handleIntercept = handleInterceptBeanMap.get(beanName);
            handleTemp.add(handleIntercept);
        }
        handleInterceptList.addAll(handleTemp);
    }
}
